﻿
using System;

namespace WindowsAPI.Windows
{
    public partial class WindowsZOrderViewer : WindowViewerControl
    {
        private readonly WindowsList f_ChildsList = new WindowsList();


        public WindowsZOrderViewer()
        {
            InitializeComponent();

            f_ChildsWindows_ListBox.DataSource = f_ChildsList;
            f_ChildsWindows_ListBox.DisplayMember = "Text";
        }


        public override void View(Window w)
        {
            base.View(w);

            f_Main_WindowInfoControl.View(w);
            f_Parent_WindowInfoControl.View(w.Parent);
            f_Child_WindowInfoControl.View(w.ZOrder.Child);
            f_Prew_WindowInfoControl.View(w.ZOrder.Prev);
            f_Next_WindowInfoControl.View(w.ZOrder.Next);

            f_ChildsList.AddRange(w.Childs);

            //foreach(var child in w.Childs)
            //    f_ChildsWindows_ListBox.Items.Add(child);
        }

        public override void Clear()
        {
            base.Clear();

            f_Main_WindowInfoControl.Clear();
            f_Parent_WindowInfoControl.Clear();
            f_Child_WindowInfoControl.Clear();
            f_Prew_WindowInfoControl.Clear();
            f_Next_WindowInfoControl.Clear();

            f_ChildsList.Clear();
        }

        private void OnWindowSelected(object sender, EventArgs e)
        {
            var lv_WindowInfoControl = sender as WindowInfoControl;
            if(lv_WindowInfoControl == null) return;

            var window = lv_WindowInfoControl.Window;
            if(window == null)
            {
                Console.Beep();
                return;
            }

            View(window);
        }

        private void f_KeyboardComponent_OnKeyDown(KeyboardComponent Sender, OnKeyboardEventArgs Args)
        {
            switch(Args.Struct.VirtualKeyCode)
            {
                case VirtualKey.A:
                    OnWindowSelected(f_Prew_WindowInfoControl, EventArgs.Empty);
                    break;
                case VirtualKey.D:
                    OnWindowSelected(f_Next_WindowInfoControl, EventArgs.Empty);
                    break;
                case VirtualKey.W:
                    OnWindowSelected(f_Parent_WindowInfoControl, EventArgs.Empty);
                    break;
                case VirtualKey.S:
                    OnWindowSelected(f_Child_WindowInfoControl, EventArgs.Empty);
                    break;
            }
        }

        private void FindWindow_Button_Click(object sender, EventArgs e)
        {
            View(Window.GetWindow(f_FindWindowText_TextBox.Text));
        }

        private void Close_Button_Click(object sender, EventArgs e)
        {
            var w = Window;
            if(w == null) return;
            w.Close();
        }

        private void Minimaze_Button_Click(object sender, EventArgs e)
        {
            var w = Window;
            if(w == null) return;
            w.Minimaze();
        }

        private void Maximize_Button_Click(object sender, EventArgs e)
        {
            var w = Window;
            if(w == null) return;
            w.Maximize();
        }

        private void Normal_Button_Click(object sender, EventArgs e)
        {
            var w = Window;
            if(w == null) return;
            w.Normal();
        }

        private void f_ChildsWindows_ListBox_DoubleClick(object sender, EventArgs e)
        {
            View(f_ChildsWindows_ListBox.SelectedItem as Window);
        }
    }
}
